
<!DOCTYPE html>
<html lang="zh-cmn-Hans">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>

<body>
    <script>
        function Observer(data) {
            this.data = data
            this.init(data)
        }
        Observer.prototype.init = function (obj) {
            let val
            for (let key in obj) {
                if (obj.hasOwnProperty(key)) {
                    val = obj[key]
                    if (typeof val === 'object') {
                        new Observer(val)
                    }
                    this.convert(key, val)
                }
            }
        }
        Object.prototype.convert = function (key, val) {
            Object.defineProperty(this.data, key, {
                enumerable: true,
                configurable: true,
                get: function () {
                    console.log(`你访问了${key}`)
                    return val
                },
                set: function (newVal) {
                    console.log(`你设置了${key}`)
                    console.log(`新的${key} 是 ${newVal}`)
                    if (newVal === val) {
                        return
                        val = newVal
                    }
                }
            })
        }
        let app1 = new Observer({
            name: 'youngwind',
            age: 25
        });
        let app2 = new Observer({
            university: 'bupt',
            major: 'computer'
        });
    </script>
</body>

</html>